package com.baoteng.rbspy.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mustache.MustacheResourceTemplateLoader;
import org.springframework.stereotype.Service;

import com.baoteng.rbspy.config.car.URLConfig;
import com.baoteng.rbspy.model.Model;
import com.baoteng.rbspy.model.request.RenewalDetailsRequest;
import com.baoteng.rbspy.model.request.VehicleModelRequest;
import com.baoteng.rbspy.model.response.ResponseModel;
import com.baoteng.rbspy.model.vehicle.VehicleModelPO;
import com.baoteng.rbspy.model.vehicle.VehiclePO;
import com.baoteng.rbspy.service.OkHttpClientService;
import com.baoteng.rbspy.service.RenewalService;
import com.baoteng.rbspy.service.VehicleService;
import com.baoteng.rbspy.util.BeanConvertUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.samskivert.mustache.Mustache;

@Service
public class VehicleServiceImpl implements VehicleService {

	@Autowired
	OkHttpClientService okHttpClientService;

	@Autowired
	private Mustache.Compiler compiler;

	@Autowired
	MustacheResourceTemplateLoader mustacheTemplateLoader;

	@Autowired
	URLConfig urlConfig;

	@Autowired
	RenewalService renewalService;

	BeanConvertUtil beanUtil = BeanConvertUtil.newInstance();

	public VehiclePO renewalVehicleInfo(RenewalDetailsRequest request) {
		ResponseModel<VehiclePO> vehicleResponse = okHttpClientService
				.getHtml(urlConfig.getVehicleUrl() + "?" + beanUtil.convert2RequestURLParam(request), VehiclePO.class);
		return vehicleResponse.isSuccessful() && vehicleResponse.getBody() != null ? vehicleResponse.getBody() : null;
	}

	public void saveVehicleInfo() {

	}

	public void cacheVehicleInfo() {

	}

	public void getVehicleInfo() {

	}
	
	public List<VehicleModelPO> getModelInfo(VehicleModelRequest request) {
		ResponseModel<Model<VehicleModelPO>> vmResponse = okHttpClientService
				.getJson(urlConfig.getVmQueryUrl() + "?" + beanUtil.convert2RequestURLParam(request), new TypeReference<Model<VehicleModelPO>>() {});
		if(vmResponse.isSuccessful() && vmResponse.getBody()!=null && vmResponse.getBody().getData()!=null && !vmResponse.getBody().getData().isEmpty())
		{
			return vmResponse.getBody().getData();
		}
		return null;
	}
	
	public String getModelCodeByVin(VehicleModelRequest request) {
		ResponseModel<Model<VehicleModelPO>> vmResponse = okHttpClientService
				.getJson(urlConfig.getVmQueryVinUrl() + "?" + beanUtil.convert2RequestURLParam(request), new TypeReference<Model<VehicleModelPO>>() {});
		if(vmResponse.isSuccessful() && vmResponse.getBody()!=null && StringUtils.isNotBlank(vmResponse.getBody().getMsg())) {
			return vmResponse.getBody().getMsg();
		}
		return null;
	}
	
}
